#include <arrow/api.h>
#include <arrow/result.h>
#include <arrow/status.h>

#include <arrow/array/data.h>
#include <arrow/array/array_base.h>

#include <iostream>
#include <vector>
#include <bitset>

using namespace arrow;
using namespace std;

arrow::Status  fill_ints() {
  // Make Arrays
  arrow::NumericBuilder<arrow::Int64Type> int64_builder;
   // Make place for 8 values in total
  ARROW_RETURN_NOT_OK(int64_builder.Resize(8));

  // Bulk append the given values
  std::vector<int64_t> int64_values = {1, 2, 3, 4, 5, 6, 7, 8};
  ARROW_RETURN_NOT_OK(int64_builder.AppendValues(int64_values));
  std::shared_ptr<arrow::Array> array_a;
  ARROW_RETURN_NOT_OK(int64_builder.Finish(&array_a));
  int64_builder.Reset();
 
  return arrow::Status::OK();
}

arrow::Array* fill_int_array() {
     // Make Arrays
  arrow::NumericBuilder<arrow::Int64Type> int64_builder;
   // Make place for 8 values in total
  int64_builder.Resize(8);

  std::vector<bool> validity = {true, true, true, true, true, true, true, true};
  // Bulk append the given values
  std::vector<int64_t> int64_values = {1, 2, 3, 4, 5, 6, 7, 8};

  validity = {true, true, true, false, true, true, true, true};
  int64_values = {1, 2, 3, 0, 5, 6, 7, 8};
  int64_builder.AppendValues(int64_values, validity);

  std::shared_ptr<arrow::Array> array_a;
  int64_builder.Finish(&array_a);
  int64_builder.Reset();

  std::vector<std::shared_ptr<arrow::Buffer>>  buffers = array_a->data()->buffers;

  std::cout<<"buffers length: " << buffers.size() << std:: endl;
  
  if (buffers[0] != NULLPTR) {
    std::cout<< "null_bitmap: length " << buffers[0]->size() << std::endl;
    // std::cout<< "null_bitmap:  hex " << buffers[0]->ToHexString() << std::endl;
      std::cout<< "null_bitmap:  二进制 [";
        for(int i =0; i < buffers[1]->size(); i++) {
          std::cout<<std::bitset<sizeof(uint8_t)*8>(buffers[0]->data()[i])<<" ";
        }
        std::cout<<"]"<<std::endl;
  }

  if(buffers[1]!=NULLPTR) {
        std::cout<< "data: length " << buffers[1]->size() << std::endl;
        // std::cout<< "data:  hex " << buffers[1]->ToHexString() << std::endl;
        // 输出数值的十六禁止
        // std::cout<< "data:  十六进制 " << hex << buffers[1]->data() << std::endl;
        std::cout<< "data:  二进制 [";
        for(int i =0; i < buffers[1]->size(); i++) {
          std::cout<<std::bitset<sizeof(uint8_t)*8>(buffers[1]->data()[i])<<" ";
        }
        std::cout<<"]"<<std::endl;

        // for(int i =0; i < buffers[1]->size(); i++) {
        //   for(int j=7; j>=0;j--) {
        //     std::cout << ((buffers[1]->data()[i]>>j) & 1);
        //   }
        //   std::cout<<" ";
        // }
        // std::cout<<std::endl;
  }
  arrow::Array* int_array = array_a.get();
  return int_array;
}

arrow::ArrayData* fill_int_array_data() {
    arrow::Array* int_array = fill_int_array();
    std::shared_ptr<arrow::ArrayData> data = int_array->data();
    arrow::ArrayData* _data = data.get();

    return _data;
}

/* void fill_int_array_data_buffer() {
    arrow::ArrayData* data = fill_int_array_data();
    std::vector<std::shared_ptr<arrow::Buffer>>  buffers = data->buffers();
} */

int main(int argc, char *argv[]) {
    arrow::ArrayData* data = fill_int_array_data();
    
    // unsigned char * const * v = data->GetValuesSafe<unsigned char*>(0);

    // std::cout << "v: " << *v << std::endl;

    std::cout << "-----------------end-----------------"<< std::endl;

    return 1;
}